import java.util.ArrayList;

import javafx.animation.FadeTransition;
import javafx.scene.layout.FlowPane;
import javafx.util.Duration;

public class Hand extends FlowPane{
	//public Card card;
	private  ArrayList<Card> cards = new ArrayList<Card>();
	private static Game delegator;
	private boolean isHide=false;
	public Hand(){
		this.setPrefWidth(700);
	}
		
	public void setDelegator(Game del){
		delegator = del;
	}
	
	public void drawWithListener(Card cd){
		this.draw(cd);
		this.setListener(cd);
	}
	
	//无监听
	public void draw(Card cd){
		cards.add(cd);
		cd.turnUp();
		this.getChildren().add(cd);
		if(isHide){
			cd.turnBack();
		}
		else{
			cd.turnUp();
		}
	}
	
	private void setListener(Card cd){
		cd.setOnMouseClicked(e -> {
			delegator.newClickOnCard(cd);
		});
		//需用代理自动计算
		cd.setOnMouseEntered(e -> {
			delegator.HangingOnCard(cd);
		});
		cd.setOnMouseExited(e -> {
			delegator.ExitCard(cd);
		});
	}
	
	
	
	public void put(Card cd){
		cards.remove(cd);		
		this.getChildren().remove(cd);
	}
	
	public void newput(Card cd){
		FadeTransition fade = new FadeTransition();
		fade.setDuration(Duration.millis(500));
		fade.setNode(cd);
		fade.setToValue(0);
		fade.play();
		fade.setOnFinished(e->{
			cards.remove(cd);
			this.getChildren().remove(cd);
			delegator.handPutFinished(cd);
		});
	}
	
	
	public void clear(){
		this.getChildren().clear();
		cards.clear();
	}
	
	public Card at(int index){
		return cards.get(index);
	}
	
	public int size(){
		return cards.size();
	}
	
	public void switchUpside(){
		if(isHide){
			for(Card cd:cards){
				cd.turnUp();
			}
			isHide=false;
		}
		else{
			for(Card cd:cards){
				cd.turnBack();;
			}
			isHide=true;
		}
	}
	
	
}
